package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.config.CheckProperties;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.wx.annotation.LoginUser;
import cn.com.haopy.yue.ai.wx.service.CheckReportService;
import cn.com.haopy.yue.ai.wx.web.base.BaseWxController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;


@RestController
@RequestMapping("/haopyYueAi/checkReport")
@Api(tags = "体检报告")
public class WxCheckReportController extends BaseWxController {

    private final Log logger = LogFactory.getLog(WxCheckReportController.class);

    @Autowired
    private CheckProperties checkProperties;

    @Autowired
    CheckReportService checkReportService;


    /**
     * 体检记录
     */
    @GetMapping("/checkList")
    @ApiOperation(value = "体检记录")
    public Object checkList(
            @LoginUser Integer userId,
            @RequestParam @ApiParam("身份证号") String idcardNo,
            @RequestParam @ApiParam("姓名") String name) {
        try {
            if (Objects.isNull(userId)) {
                return ResponseUtil.unlogin();
            }
            Object list = checkReportService.checkList(idcardNo, name);
            return ResponseUtil.ok(list);
        } catch (Exception e) {
            return ResponseUtil.failMsg(e.getMessage());
        }
    }

    /**
     * 体检报告详细数据
     */
    @GetMapping("/checkReportData")
    @ApiOperation(value = "体检报告详细数据")
    public Object checkReportData(
            @LoginUser Integer userId,
            @RequestParam @ApiParam("体检编号") String workNo) {
        try {
            if (Objects.isNull(userId)) {
                return ResponseUtil.unlogin();
            }
            Object data = checkReportService.checkDetailData(workNo);
            return ResponseUtil.ok(data);
        } catch (Exception e) {
            return ResponseUtil.failMsg(e.getMessage());
        }
    }


    /*
    @ApiOperation("读取体检报告文件")
    @GetMapping("/reportPdfFile")
    public void reportPdfFile(@RequestParam @ApiParam("报告编号") String pdfPath) {

        response.setContentType("application/pdf;charset=UTF-8");
        OutputStream out;

        try {
            URL url = new URL(checkProperties.getHost() + pdfPath);
            out = response.getOutputStream();
            byte[] data = Base64Url.encodeImageToInByte(url);
            out.write(data);
            out.flush();
            out.close();
            //return ResponseUtil.ok();
        } catch (IOException e) {
            logger.error("读物体检报告失败，错误：" + e.getMessage());
        } catch (Exception e) {
            logger.error("读物体检报告失败，错误：" + e.getMessage());
            e.printStackTrace();
            //return ResponseUtil.failMsg(e.getMessage());
        }

        //return ResponseUtil.failMsg("读物体检报告失败");
    }

     */


}
